<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>轮播</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.5.4/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <blockquote class="layui-elem-quote layui-text">
            在此可以编辑轮播
        </blockquote>

        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>编辑轮播</legend>
        </fieldset>

        <form class="layui-form">

            <input type="hidden" name="id" value="{{$data['id']}}">

            <div class="layui-form-item">
                <label class="layui-form-label">跳转路径</label>
                <div class="layui-input-inline">
                    <input type="text" name="path" lay-verify="title" value="{{$data['path']}}" autocomplete="off"
                           placeholder="请输入标题"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">图片</label>
                <div class="layui-input-inline">
                    <img   id="img_url"  width="100px" height="80px"  src="{{$data['img_url']}}">
                </div>
                <input hidden name="img_url" value="{{$data['img_url']}}">

            </div>
            <button type="button" class="layui-btn" id="test1">
                <i class="layui-icon">&#xe67c;</i>上传图片
            </button>

            <div id="test12" class="demo-tree-more"></div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
                </div>
            </div>
        </form>

    </div>
</div>

<script src="/lib/layui-v2.5.4/layui.js" charset="utf-8"></script>
<script src="/js/jquery-3.4.1.min.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'layedit', 'laydate', 'upload'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            , upload = layui.upload;

       var  uploadInst = upload.render({
             elem: '#test1' //绑定元素
            , url: '/admin/upload/img' //上传接口
            , done: function (res) {
                $('[name="img_url"]').val(res.url);
                $('#img_url').attr('src',res.url)
            }
            , error: function (re) {
                //请求异常回调
               layer.msg('上传失败', {icon: 2});
            }
        });
        var  uploadInst = upload.render({
            elem: '#test1' //绑定元素
            , url: '/admin/upload/img' //上传接口
            , done: function (res) {
                $('[name="head"]').val(res.url);
                $('#head').attr('src',res.url)
            }
            , error: function (re) {
                //请求异常回调
                layer.msg('上传失败', {icon: 2});
            }
        });
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });


        var editIndex = layedit.build('demo',{
            height: 180,
        });

        //自定义验证规则
        form.verify({
            content: function (value) {
                layedit.sync(editIndex);
            }
        });



        //监听提交
        var url = '/admin/banner/save';
        form.on('submit(demo1)', function (data) {

            var formData = data.field;
            $.ajax({
                url: url,
                data: formData,
                method: 'post',
                headers: {
                    'X-CSRF-TOKEN': '{{ csrf_token() }}'
                },
                success: function (resp, status, xhr) {
                    if (xhr.status == 200) {
                        // var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        // //layer.msg(index);
                        // parent.layer.close(index); //再执行关闭
                        // window.parent.location.reload();
                        location.href = '/admin/banner/index';
                    }
                },
                error: function (re) {
                    layer.msg(re.responseJSON.msg, {icon: 2});

                }
            })


            return false;

        });

    });
</script>

</body>
</html>